// pages/hot-comment/hot-comment.js
// pages/index/index.js
import request from '../../request/index'
//使用 async await 语法必须引入，固定写法。
import regeneratorRuntime from '../../utils/runtime'
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotComment: [],//热评数据
    index: 0,//当前的热评下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(App.globalData.backgroundAudioManager && !App.globalData.backgroundAudioManager.paused){
      App.globalData.backgroundAudioManager.pause()
    }
    this.getHotCommentData()
  },
  //获取今日热评数据
  async getHotCommentData() {
    const result = await request({ url: 'comment/hotwall/list' })
    if (result.code !== 200) {
      console.log('获取今日热评失败', result)
      return
    }
    const hotComment = result.data
    this.setData({ hotComment },
      _ => {
        this.playSong(hotComment[this.data.index].simpleResourceInfo.songId)
      })
  },
  //播放歌曲
  async playSong(id) {
    if (this.innerAudioContext) {
      this.innerAudioContext.destroy()
    }
    this.innerAudioContext = wx.createInnerAudioContext()
    this.innerAudioContext.autoplay = true//自动播放
    this.innerAudioContext.loop = true//循环播放
    this.innerAudioContext.src =
    `https://music.163.com/song/media/outer/url?id=${id}.mp3`
    this.innerAudioContext.onError((res) => {
      wx.showToast({
        title: '获取歌曲失败~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
    })
  },
  //滑动手势开始(滑动切换热评)
  pointerLocation: {
    startX: 0,//手指x轴起始位置
    endX: 0,//手指x轴结束位置
  },
  //1.触摸动作开始
  touchstart(event) {
    // console.log("开始", event)
    this.pointerLocation.startX = event.touches[0].pageX
    this.pointerLocation.endX = event.touches[0].pageX
  },
  //2.触摸后开始移动
  //做一个简单的防抖处理
  timer: null,//定时器默认值
  touchmove(event) {
    clearTimeout(this.timer)//清除定时器
    this.timer = setTimeout(_ => {
      // console.log("移动", event)
      this.pointerLocation.endX = event.touches[0].pageX
    }, 50)
  },
  //3.触摸动作结束
  touchend(event) {
    setTimeout(_ => {
      // console.log("结束", event)
      //1.下一热评
      if (this.pointerLocation.startX -
        this.pointerLocation.endX > 10
      ) {
        if (this.data.index === this.data.hotComment.length - 1) {
          wx.showToast({
            title: '已经到底啦~',
            icon: 'none',
            duration: 1000,
            mask: false
          })
          return
        }
        this.setData({ index: this.data.index + 1 },
          _ => {
            this.playSong(this.data.hotComment[this.data.index].simpleResourceInfo.songId)
          })
      } else if (this.pointerLocation.startX -
        this.pointerLocation.endX < -10) {
        //2.商议热评
        if (this.data.index === 0) {
          wx.showToast({
            title: '已经是第一首啦~',
            icon: 'none',
            duration: 1000,
            mask: false
          })
          return
        }
        this.setData({ index: this.data.index - 1 },
          _ => {
            this.playSong(this.data.hotComment[this.data.index].simpleResourceInfo.songId)
          })
      }
    }, 51)
  },
  //滑动手势结束


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.innerAudioContext) {
      this.innerAudioContext.destroy()
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //卸载页面时销毁播放器
    if (this.innerAudioContext) {
      this.innerAudioContext.destroy()
    }
    if(App.globalData.backgroundAudioManager && App.globalData.backgroundAudioManager.paused){
      App.globalData.backgroundAudioManager.play()
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})